package com.example.newsbackgroundmanagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.newsbackgroundmanagement.entity.BaseModel;
import com.example.newsbackgroundmanagement.entity.Collectiblepublicnews;
import com.example.newsbackgroundmanagement.entity.Releasenews;
import com.example.newsbackgroundmanagement.service.ICollectiblepublicnewsService;
import com.example.newsbackgroundmanagement.service.impl.CollectiblepublicnewsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Random;

import static com.example.newsbackgroundmanagement.util.DateUtil.getStringDate;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 邓旭波
 * @since 2023-06-26
 */
@Controller
@RequestMapping("/collectiblepublicnews")
public class CollectiblepublicnewsController {


    @Autowired
    ICollectiblepublicnewsService iCollectiblepublicnewsService;

    BaseModel baseModel;


    @ResponseBody
    @RequestMapping(value = "/InsertCollectiblePublicNews")//插入一条用户收藏的公共新闻
    public BaseModel InsertCollectiblePublicNews(@RequestBody Collectiblepublicnews collectiblepublicnews){
        System.out.println("-----------进入CollectiblepublicnewsController的InsertCollectiblePublicNews!");
        System.out.println("--------------------"+collectiblepublicnews);
        boolean flag=iCollectiblepublicnewsService.save(collectiblepublicnews);
        if(flag==true){
            System.out.println("------插入成功");
            baseModel = new BaseModel(121, "插入用户收藏的公共新闻成功!");
            baseModel.setData(collectiblepublicnews);
        }else if(flag==false){
            System.out.println("------插入失败");
            baseModel = new BaseModel(122, "插入用户收藏的公共新闻失败!");
        }
        return baseModel;
    }



    @ResponseBody
    @RequestMapping(value = "/DeleteCollectiblePublicNewsByUid")//根据uid删除用户收藏的公共新闻
    public BaseModel DeleteCollectiblePublicNewsByUid(@RequestBody Collectiblepublicnews collectiblepublicnews){
        System.out.println("-----------进入CollectiblepublicnewsController的DeleteCollectiblePublicNewsByUid!");
        System.out.println("--------------------"+collectiblepublicnews);
        QueryWrapper<Collectiblepublicnews> collectiblepublicnewsQueryWrapper=new QueryWrapper();
        collectiblepublicnewsQueryWrapper.eq("uid",collectiblepublicnews.getUid());
        boolean flag1=iCollectiblepublicnewsService.remove(collectiblepublicnewsQueryWrapper);
        if (flag1==true){
            System.out.println("------删除成功");
            baseModel = new BaseModel(131, "删除用户收藏的公共新闻成功!");
        }else if(flag1==false){
            System.out.println("------删除失败");
            baseModel = new BaseModel(132, "删除用户收藏的公共新闻失败!");
        }
        return  baseModel;
    }



    @ResponseBody
    @RequestMapping(value = "/DeleteCollectiblePublicNewsByUniqueKey")//根据uniquekey删除用户收藏的公共新闻
    public BaseModel DeleteCollectiblePublicNewsByUniqueKey(@RequestBody Collectiblepublicnews collectiblepublicnews){
        System.out.println("-----------进入CollectiblepublicnewsController的DeleteCollectiblePublicNewsByUniqueKey!");
        System.out.println("--------------------"+collectiblepublicnews);
        QueryWrapper<Collectiblepublicnews> collectiblepublicnewsQueryWrapper=new QueryWrapper();
        collectiblepublicnewsQueryWrapper.eq("uniquekey",collectiblepublicnews.getUniquekey());
        boolean flag1=iCollectiblepublicnewsService.remove(collectiblepublicnewsQueryWrapper);
        if (flag1==true){
            System.out.println("------根据uniquekey删除成功");
            baseModel = new BaseModel(191, "根据uniquekey删除用户收藏的公共新闻成功!");
        }else if(flag1==false){
            System.out.println("------根据uniquekey删除失败");
            baseModel = new BaseModel(192, "根据uniquekey删除用户收藏的公共新闻失败!");
        }
        return  baseModel;
    }


    @ResponseBody
    @RequestMapping(value = "/SelectCollectiblePublicNews")//查询所有用户收藏的公共新闻
    public BaseModel SelectCollectiblePublicNews(){
        System.out.println("-----------进入CollectiblepublicnewsController的SelectCollectiblePublicNews!");
        QueryWrapper<Collectiblepublicnews> collectiblepublicnewsQueryWrapper=new QueryWrapper();
        List<Collectiblepublicnews> collectiblepublicnews=iCollectiblepublicnewsService.list(collectiblepublicnewsQueryWrapper);
        if(collectiblepublicnews!=null){
            System.out.println("------查询成功");
            baseModel = new BaseModel(251, "查询所有用户收藏的公共新闻成功!");
            baseModel.setDatas(collectiblepublicnews);
        }else{
            System.out.println("------查询失败");
            baseModel = new BaseModel(252, "查询所有用户收藏的公共新闻失败!");
        }
        return baseModel;
    }

    @ResponseBody
    @RequestMapping(value = "/SelectCollectiblePublicNewsByUid")//根据uid查询用户收藏的公共新闻
    public BaseModel SelectCollectiblePublicNewsByUid(@RequestBody Collectiblepublicnews collectiblepublicnews){
        System.out.println("-----------进入CollectiblepublicnewsController的SelectCollectiblePublicNewsByUid!");
        System.out.println("--------------------"+collectiblepublicnews);
        QueryWrapper<Collectiblepublicnews> collectiblepublicnewsQueryWrapper=new QueryWrapper();
        collectiblepublicnewsQueryWrapper.eq("uid",collectiblepublicnews.getUid());
        List<Collectiblepublicnews> collectiblepublicnews1=iCollectiblepublicnewsService.list(collectiblepublicnewsQueryWrapper);
        if(collectiblepublicnews1!=null){
            System.out.println("------查询成功");
            baseModel = new BaseModel(141, "查询用户收藏的公共新闻成功!");
            baseModel.setDatas(collectiblepublicnews1);
        }else{
            System.out.println("------查询失败");
            baseModel = new BaseModel(142, "查询用户收藏的公共新闻失败!");
        }
        return baseModel;
    }



    @ResponseBody
    @RequestMapping(value = "/SelectCollectiblePublicNewsByCollectState")//根据collectstate查询用户收藏的公共新闻
    public BaseModel SelectCollectiblePublicNewsByCollectState(@RequestBody Collectiblepublicnews collectiblepublicnews){
        System.out.println("-----------进入CollectiblepublicnewsController的SelectCollectiblePublicNewsByCollectState!");
        System.out.println("--------------------"+collectiblepublicnews);
        QueryWrapper<Collectiblepublicnews> collectiblepublicnewsQueryWrapper=new QueryWrapper();
        collectiblepublicnewsQueryWrapper.eq("collectstate",collectiblepublicnews.getCollectstate());
        List<Collectiblepublicnews> collectiblepublicnews1=iCollectiblepublicnewsService.list(collectiblepublicnewsQueryWrapper);
        if(collectiblepublicnews1!=null){
            System.out.println("------根据状态查询成功");
            baseModel = new BaseModel(251, "根据状态查询用户收藏的公共新闻成功!");
            baseModel.setDatas(collectiblepublicnews1);
        }else{
            System.out.println("------根据状态查询失败");
            baseModel = new BaseModel(252, "根据状态查询用户收藏的公共新闻失败!");
        }
        return baseModel;
    }


    @ResponseBody
    @RequestMapping(value = "/UpdateCollectiblePublicNewsCollectStateByUid")//根据uid修改用户收藏的公共新闻的状态
    public BaseModel UpdateCollectiblePublicNewsCollectStateByUid(@RequestBody Collectiblepublicnews collectiblepublicnews){
        System.out.println("-----------进入CollectiblepublicnewsController的UpdateCollectiblePublicNewsCollectStateByUid!");
        System.out.println("--------------------"+collectiblepublicnews);
        UpdateWrapper<Collectiblepublicnews> collectiblepublicnewsUpdateWrapper=new  UpdateWrapper();
        collectiblepublicnewsUpdateWrapper.set("collectstate",collectiblepublicnews.getCollectstate()).eq("uid",collectiblepublicnews.getUid());
        boolean g=iCollectiblepublicnewsService.update(collectiblepublicnewsUpdateWrapper);
        if(g==true){
            System.out.println("------根据用户ID修改状态成功");
            baseModel = new BaseModel(161, "根据用户ID修改用户收藏的公共新闻的状态成功!");
        }else{
            System.out.println("------根据用户ID修改状态失败");
            baseModel = new BaseModel(162, "根据用户ID修改用户收藏的公共新闻的状态失败!");
        }
        return baseModel;
    }


    @ResponseBody
    @RequestMapping(value = "/UpdateCollectiblePublicNewsCollectStateByUniqueKey")//根据uniquekey修改用户收藏的新闻的状态
    public BaseModel UpdateCollectiblePublicNewsCollectStateByUniqueKey(@RequestBody Collectiblepublicnews collectiblepublicnews){
        System.out.println("-----------进入CollectiblepublicnewsController的UpdateCollectiblePublicNewsCollectStateByUniqueKey!");
        System.out.println("--------------------"+collectiblepublicnews);
        UpdateWrapper<Collectiblepublicnews> collectiblepublicnewsUpdateWrapper=new  UpdateWrapper();
        collectiblepublicnewsUpdateWrapper.set("collectstate",collectiblepublicnews.getCollectstate()).eq("uniquekey",collectiblepublicnews.getUniquekey());
        boolean h=iCollectiblepublicnewsService.update(collectiblepublicnewsUpdateWrapper);
        if(h==true){
            System.out.println("------根据uniquekey修改状态成功");
            baseModel = new BaseModel(171, "根据uniquekey修改用户收藏的公共新闻的状态成功!");
        }else{
            System.out.println("------根据uniquekey修改状态失败");
            baseModel = new BaseModel(172, "根据uniquekey修改用户收藏的公共新闻的状态失败!");
        }
        return baseModel;
    }




    @ResponseBody
    @RequestMapping(value = "/UpdateCollectiblePublicNews")//修改用户收藏的公共新闻
    public BaseModel UpdateCollectiblePublicNews(@RequestBody Collectiblepublicnews collectiblepublicnews){
        System.out.println("-----------进入CollectiblepublicnewsController的UpdateCollectiblePublicNews!");
        System.out.println("--------------------"+collectiblepublicnews);
        QueryWrapper<Collectiblepublicnews> collectiblepublicnewsQueryWrapper=new QueryWrapper<>();
        collectiblepublicnewsQueryWrapper.eq("uniquekey",collectiblepublicnews.getUniquekey());
        if (iCollectiblepublicnewsService.getOne(collectiblepublicnewsQueryWrapper)!=null) {
            boolean c = iCollectiblepublicnewsService.updateById(collectiblepublicnews);
            if (c == true) {
                System.out.println("------修改成功");
                baseModel = new BaseModel(261, "修改用户收藏的公共新闻成功!");
            } else if (c == false) {
                System.out.println("------修改失败");
                baseModel = new BaseModel(262, "修改用户收藏的公共新闻失败!");
            }
        }else{
            baseModel = new BaseModel(263, "该用户收藏的公共新闻不存在!");
        }
        return baseModel;
    }

}
